﻿using System;
using System.Globalization;
using System.Text.RegularExpressions;

namespace YueRu.Common
{
    class DateTimeExtend
    {
        /// <summary>
        /// 返回标准日期格式string
        /// </summary>
        public static string GetDate()
        {
            return DateTime.Now.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 返回指定日期格式
        /// </summary>
        public static string GetDate(string datetimestr, string replacestr)
        {
            if (datetimestr == null)
                return replacestr;

            if (datetimestr.Equals(""))
                return replacestr;

            try
            {
                datetimestr = Convert.ToDateTime(datetimestr).ToString("yyyy-MM-dd").Replace("1900-01-01", replacestr);
            }
            catch
            {
                return replacestr;
            }
            return datetimestr;
        }


        /// <summary>
        /// 返回标准时间格式string
        /// </summary>
        public static string GetTime()
        {
            return DateTime.Now.ToString("HH:mm:ss");
        }

        /// <summary>
        /// 返回标准时间格式string
        /// </summary>
        public static string GetDateTime()
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// 返回相对于当前时间的相对天数
        /// </summary>
        public static string GetDateTime(int relativeday)
        {
            return DateTime.Now.AddDays(relativeday).ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// 返回标准时间格式string
        /// </summary>
        public static string GetDateTimeF()
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fffffff");
        }

        /// <summary>
        /// 返回标准时间
        /// </summary>
        public static string GetStandardDateTime(string fDateTime, string formatStr)
        {
            if (fDateTime == "0000-0-0 0:00:00")
            {
                return fDateTime;
            }
            DateTime time;
            return DateTime.TryParse(fDateTime, out time) ? time.ToString(formatStr) : "N/A";
        }

        /// <summary>
        /// 返回标准时间 yyyy-MM-dd HH:mm:ss
        /// </summary>
        public static string GetStandardDateTime(string fDateTime)
        {
            return GetStandardDateTime(fDateTime, "yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// 返回标准时间 yyyy-MM-dd
        /// </summary>
        public static string GetStandardDate(string fDate)
        {
            return GetStandardDateTime(fDate, "yyyy-MM-dd");
        }

        /// <summary>
        /// 是否正确的时间格式字符串
        /// </summary>
        /// <returns></returns>
        public static bool IsTime(string timeval)
        {
            return Regex.IsMatch(timeval, @"^((([0-1]?[0-9])|(2[0-3])):([0-5]?[0-9])(:[0-5]?[0-9])?)$");
        }

        /// <summary>
        /// 返回相差的秒数
        /// </summary>
        /// <param name="time"></param>
        /// <param name="sec"></param>
        /// <returns></returns>
        public static int StrDateDiffSeconds(string time, int sec)
        {
            if (String.IsNullOrEmpty(time))
            {
                return 1;
            }
            DateTime dateTime = TypeConverter.StrToDateTime(time, DateTime.Parse("1900-01-01"));
            if (dateTime.ToString("yyyy-MM-dd") == "1900-01-01")
            {
                return 1;
            }

            TimeSpan ts = DateTime.Now - dateTime.AddSeconds(sec);
            if (ts.TotalSeconds > Int32.MaxValue)
            {
                return Int32.MaxValue;
            }

            if (ts.TotalSeconds < Int32.MinValue)
            {
                return Int32.MinValue;
            }

            return (int)ts.TotalSeconds;
        }

        /// <summary>
        /// 返回相差的分钟数
        /// </summary>
        /// <param name="time"></param>
        /// <param name="minutes"></param>
        /// <returns></returns>
        public static int StrDateDiffMinutes(string time, int minutes)
        {
            if (String.IsNullOrEmpty(time))
            {
                return 1;
            }
            DateTime dateTime = TypeConverter.StrToDateTime(time, DateTime.Parse("1900-01-01"));
            if (dateTime.ToString("yyyy-MM-dd") == "1900-01-01")
            {
                return 1;
            }

            TimeSpan ts = DateTime.Now - dateTime.AddMinutes(minutes);
            if (ts.TotalMinutes > Int32.MaxValue)
            {
                return Int32.MaxValue;
            }
            if (ts.TotalMinutes < Int32.MinValue)
            {
                return Int32.MinValue;
            }

            return (int)ts.TotalMinutes;
        }

        /// <summary>
        /// 返回相差的小时数
        /// </summary>
        /// <param name="time"></param>
        /// <param name="hours"></param>
        /// <returns></returns>
        public static int StrDateDiffHours(string time, int hours)
        {
            if (String.IsNullOrEmpty(time))
            {
                return 1;
            }

            DateTime dateTime = TypeConverter.StrToDateTime(time, DateTime.Parse("1900-01-01"));
            if (dateTime.ToString("yyyy-MM-dd") == "1900-01-01")
            {
                return 1;
            }

            TimeSpan ts = DateTime.Now - dateTime.AddHours(hours);
            if (ts.TotalHours > Int32.MaxValue)
            {
                return Int32.MaxValue;
            }
            if (ts.TotalHours < Int32.MinValue)
            {
                return Int32.MinValue;
            }

            return (int)ts.TotalHours;
        }

        /// <summary>
        /// 判断字符串是否是yy-mm-dd字符串
        /// </summary>
        /// <param name="str">待判断字符串</param>
        /// <returns>判断结果</returns>
        public static bool IsDateString(string str)
        {
            return Regex.IsMatch(str, @"(\d{4})-(\d{1,2})-(\d{1,2})");
        }


        /// <summary>
        ///  将8位日期型整型数据转换为日期字符串数据
        /// </summary>
        /// <param name="date">整型日期</param>
        /// <param name="chnType">是否以中文年月日输出</param>
        /// <returns></returns>
        public static string FormatDate(int date, bool chnType = false)
        {
            var dateStr = date.ToString(CultureInfo.InvariantCulture);

            if (date <= 0 || dateStr.Length != 8)
            {
                return dateStr;
            }

            if (chnType)
            {
                return dateStr.Substring(0, 4) + "年" + dateStr.Substring(4, 2) + "月" + dateStr.Substring(6) + "日";
            }
            return dateStr.Substring(0, 4) + "-" + dateStr.Substring(4, 2) + "-" + dateStr.Substring(6);
        }
    }
}
